package br.financeiro.utils;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class AndroidUtils {

	public static void showMessage(Context context, String title, String message) {
		showMessage(context, title, message, android.R.drawable.ic_dialog_info);
	}

	public static void showMessage(Context context, String title,
			String message, int icon) {
		Builder messager = new Builder(context);
		messager.setTitle(title);
		messager.setMessage(message);
		messager.setIcon(icon);
		messager.setPositiveButton("OK", null);
		messager.show();
	}

	public static void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			// pre-condition
			return;
		}

		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}
}
